;(function($,ECar){
/*
 * Copyright (C), 2013-2013, 上海汽车集团股份有限公司
 * Author:   王景亮
 * Date:     2015-4-16
 * Description: 注册登录页面 JS
 *
*/
	// 获取所在城市
	$.getJSON("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=&callback=?", function(data) {
		var cityName = "";
		var cityCode = "";
		if(data){
			if(data.status==0) {
				cityName = data.address.split("|")[2];
				cityCode = data.content.address_detail.city_code;
				$("#cityName").val(cityName);
				$("#cityCode").val(cityCode);
			}
		}
		
	});
	
	new ECar.validate("#register",{
	  errClass:'red',
	  addWhere:true,
	  successtext:'<i class="carpay-icon corr-icon"></i>',
	  submit:function(){
		  if($("#checkCodeMsg").find(".corr-icon").length>0){
			  return true;
		  }
		  return false;
	  },
	  fail:function(){
		  try{
			 console.info('fail');
		  } catch(e){}
	  }
	});
	
	$("#checkCode").blur(function(){
		var userName = $("#userName").val();
		var checkCode = $("#checkCode").val();
		 if(checkCode.trim()==""){
			 $("#checkCodeMsg").html("<i class=\"carpay-icon erro-icon\"></i>"+"<span class=\"pro-cor-erro\">为6位验证码</span>").parent().show();
		     $("#checkCode").addClass("red");
			 return;
		 }
		 var regex = /^\d{6}$/;
		 if(!(new RegExp(regex)).test(checkCode)){
			 $("#checkCodeMsg").html("<i class=\"carpay-icon erro-icon\"></i>"+"<span class=\"pro-cor-erro\">为6位验证码</span>").parent().show();
			 $("#checkCode").addClass("red");
			 return;
		 }
		 
		 if(userName.trim()!=""&&checkCode.trim()!=""){
			 $.ajax({
					type: "POST",
					url: base+'/pay/validate/toValidateCheckCode.htm',
					async: false,
					data: {
						userName:$("#userName").val(),
						checkCode:$("#checkCode").val()
					} ,
					success: function(data){
						if(data=="false"){
							$("#checkCodeMsg").html("<i class=\"carpay-icon erro-icon\"></i>"+"<span class=\"pro-cor-erro\">验证码不正确，请核对后重新填写</span>").parent().show();
							$("#checkCode").addClass("red");
						}else{
							$("#checkCodeMsg").html("<i class=\"carpay-icon corr-icon\"></i>").parent().show();
							$("#checkCode").removeClass("red");
						}
				    }
				});	
		 }else{
			 $("#checkCodeMsg").html("<i class=\"carpay-icon erro-icon\"></i>"+"<span class=\"pro-cor-erro\">验证码不正确，请核对后重新填写</span>").parent().show();
			 $("#checkCode").addClass("red");
		 }
		
	})

	var getcode=function(url,data,fn){
		var fn=fn||$.noop;
		$.ajax({
			type:'POST',
			   url:url,
			   data:{'userName':data, 'msgCode':'10001'},  //ACCT_REG_01
			   dataType:'json',
			   success:function(d){
				   fn.call(this,d)
			   }
		});
	};
	var flag=false,gain=$('.gain');
	if(flag){
		countDown({
			obj:gain,
			wait:50,
			txt:'秒',
			callback:function(){
				//$this.text(oldtxt);
			}
		});
	}

	gain.bind('click',function(){
		var $this=$(this);
		var url = base+'/pay/validate/sendPhoneCheckCode.htm';		
		if($this.attr('disabled'))return;
		getcode(url,$("#userName").val(),function(data){
			 if(data.errorCode=="1"){
				 $(".password-text").html("<i class=\"carpay-icon corr-icon\"></i>"+data.errorMessage).parent().show();
			 }
			 else if(data.errorCode != "1"&&data.errorMessage != ""){
				$(".password-text").html("<i class=\"carpay-icon erro-icon\"></i>"+"<span class=\"pro-cor-erro\">"+data.errorMessage+"</span>").parent().show();
			}
		});
		countDown({
			obj:$this,
			wait:60,
			txt:'秒',
			callback:function(){
				//$this.text(oldtxt);
			}
		});
	});

	var passwordId=$('#password');
	passwordId.on('keyup',function(){
		var $this=$(this),txt=$this.val(),str=['弱','中','强'];
		$("#passwordLevel").removeClass("dsp-n");
		$('em.str').text(str[pwdStrong(txt)]);
	});

	var pwdStrong=function(txt){
		var len=txt.length;
		if(len<6){
			return 0;
		}else{
			return txt.length>=12?2:1;
		}		
	};
	if(!!$.trim($("#userName").val())){
		$("#gain").removeClass("colorccc").removeAttr("disabled");
	}
	
	/**
	 * add-20150707
	 */
    $(".remember span").toggle(function() {
        $(this).removeClass("ticks carpay-icon").addClass('check');
        $(".remember .checkbox").attr("checked", false);         
    }, function() {
        $(this).addClass("ticks carpay-icon").removeClass('check');
        $(".remember .checkbox").attr("checked", true);
    });
    $("#agreement-a").on("click",function(e){
    	showDialog('agreement');
    	e.preventDefault();
    	e.stopPropagation();
    });
	
	/**
	 * [显示弹出框]
	 * @param  {[String]} type
	 */
	function showDialog(type){
		var temArr = ['agreement'];
		if(!!type&&temArr.join().indexOf(type)!=-1){
			$("#" + type).removeClass("dialog-hiden").addClass('dialog-show');
			$(".exp-dialog").removeClass("dialog-hiden").addClass('dialog-show');
			$(".mask").removeClass("dialog-hiden").addClass('dialog-show');
			$(".close-btn").bind("click", {'type': type}, hideDialog);
			//滚动条
			new ECar.imitatescroll({
			    id: "#agreement"
			});
		}
	}

	/**
	 * [清除弹出框]
	 * @param  {[type]} e
	 */
	function hideDialog(e){
		$("#" + e.data.type).removeClass("dialog-show").addClass('dialog-hiden');
		$('.exp-dialog').removeClass("dialog-show").addClass('dialog-hiden');
		$('.mask').removeClass("dialog-show").addClass('dialog-hiden');
		$(".close-btn").unbind("click");
		e.preventDefault();
		e.stopPropagation();
	}

})(jQuery,window.ECar||(window.ECar={}));